package com.grao.waterschool.human.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.grao.waterschool.human.common.R;
import com.grao.waterschool.human.entiry.po.OdsPksj;
import com.grao.waterschool.human.entiry.vo.CourseListInfo;
import com.grao.waterschool.human.entiry.vo.JsVo;
import com.grao.waterschool.human.entiry.vo.KcVo;
import com.grao.waterschool.human.entiry.vo.SelectCourseCriteria;
import com.grao.waterschool.human.manager.service.CourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "课程表信息搜索")
@RequestMapping("/course/")
@RestController
@Slf4j
public class CourseController {

    private final CourseService courseService;

    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    @Operation(summary = "去数据中台同步课程表 / 不再使用")
    @SaCheckPermission(orRole = {"super-admin, admin"})
    @PostMapping("getcourse")
    public R<String> getCourseList() {
        // 同步课程表前要先清空数据表
        R<String> stringR = courseService.truncateTable();
        return stringR.getStatus() == 0 ? R.success("课程同步成功") : R.error("课程同步失败");
    }

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "根据周次、教师号、教室号、节次搜索课程")
    @PostMapping("selectCourseList")
    public R selectCourseList(@RequestBody SelectCourseCriteria selectCourseCriteria) {
        log.info("查询条件：{}", selectCourseCriteria);
        Page<CourseListInfo> courseList = courseService.selectCourseList(selectCourseCriteria);
        return R.success(courseList);
    }

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "获取教师号列表")
    @GetMapping("jghList")
    public R getJGHList() {
        List<JsVo> jghList = courseService.selectJGHList();
        if (jghList.isEmpty()) {
            return R.error("教师号为空，请重新导入课程数据");
        }
        return R.success(jghList);
    }

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "获取课程号列表")
    @GetMapping("kchList")
    public R getKCHList() {
        List<KcVo> kchList = courseService.selectKCHList();
        if (kchList.isEmpty()) {
            return R.error("课程号为空，请重新导入课程数据");
        }
        return R.success(kchList);
    }

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "获取周次")
    @GetMapping("zcList")
    public R<List<String>> getZCList() {
        List<String> zcList = courseService.selectZCList();
        if (zcList.isEmpty()) {
            return R.error("周次为空，请重新导入课程数据");
        }
        return R.success(zcList);
    }

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "获取教室号列表")
    @GetMapping("jshList")
    public R<List<String>> getJSHList() {
        List<String> jshList = courseService.selectJSHList();
        if (jshList.isEmpty()) {
            return R.error("教室号为空，请重新导入课程数据");
        }
        return R.success(jshList);
    }

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "查询全部院系")
    @GetMapping("getYxList")
    public R getYxList() {
        List<String> yxList = courseService.getYxList();
        return R.success(yxList);
    }

    @SaCheckPermission(orRole = {"super-admin, admin"})
    @Operation(summary = "导入教师信息")
    @GetMapping("excelJsxx")
    public R excelJsxx(@RequestParam("file") MultipartFile file) throws IOException {
        Workbook workbook = new XSSFWorkbook(file.getInputStream());
        Sheet sheetAt = workbook.getSheetAt(0);
        Map<String, String> course = new HashMap<>();
        for (int i = 0; i < sheetAt.getLastRowNum(); i++) {
            Row row = sheetAt.getRow(i);
            course.put(row.getCell(0).toString(), row.getCell(1).toString());
        }
        List<List<OdsPksj>> list = new ArrayList<>();
        List<OdsPksj> odsPksjsList = courseService.getList();
        if (odsPksjsList.size() > 1000) {
            int size = odsPksjsList.size() / 1000;
            if (odsPksjsList.size() % 1000 > 0) {
                size += 1;
            }
            int start = 0;
            int end = 1000;
            for (int i = 0; i < size; i++) {
                if (i == size - 1) {
                    List<OdsPksj> odsPksjList = odsPksjsList.subList(start, odsPksjsList.size());
                    list.add(odsPksjList);
                } else {
                    start = i * 1000;
                    end = start + 1000;
                    List<OdsPksj> odsPksjList = odsPksjsList.subList(start, end);
                    list.add(odsPksjList);
                }
            }
        }
        for (List<OdsPksj> odsPksjList : list) {
            for (OdsPksj odsPksj : odsPksjList) {
                odsPksj.setJsmc(course.get(odsPksj.getJgh()));
            }
            courseService.update(odsPksjList);
        }
        workbook.close();
        return R.success();
    }
}
